/**
 * @Author 天下无敌
 * @Date 
 * @AIDE AIDE+ 
 */
package com.ecs.core.screen;
import com.badlogic.gdx.ScreenAdapter;
import com.ecs.core.MainGame;
import com.artemis.World;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.ecs.core.GameContext;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.artemis.WorldConfigurationBuilder;
import com.ecs.core.gsystem.StageSystem;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.Gdx;
import com.ecs.core.gsystem.CameraSystem;
import com.ecs.core.gsystem.RenderContextSystem;
import com.ecs.core.gsystem.player.PlayerDataSystem;
import com.ecs.core.gsystem.chunk.ChunkSystem;
import com.ecs.core.gsystem.AssetsSystem;
import com.ecs.core.debug.DebugBoxRenderSystem;
import com.ecs.core.system.PendingDeleteSystem;
import com.badlogic.gdx.InputMultiplexer;
import com.ecs.core.debug.DebugUiSystem;
import com.ecs.core.gsystem.chunk.ChunkGeneraterSystem;
import com.ecs.core.gsystem.draw.SpriteRenderSystem;
import com.ecs.core.gsystem.player.CamerafollPlayerPosSystem;
import com.ecs.core.gsystem.player.PlayerRockerStystem;
import com.ecs.core.system.MovementSystem;
import com.ecs.core.gsystem.draw.AnimationSystem;
import com.ecs.core.system.StateSystem;
import com.ecs.core.gsystem.chunk.ChunkUnloadSystem;
import com.ecs.core.gsystem.NearTagSystem;
import com.ecs.core.debug.NearRenderSystem;
import com.ecs.core.gsystem.player.PlayerTerrainStateSystem;

public class GameScreen extends ScreenAdapter{
	
	MainGame game;
	final World world;
	final OrthographicCamera camera;
	final Stage stage;
	final InputMultiplexer inputMultiplexer;
	
	public GameScreen(MainGame game)
	{
		this.game = game;
		camera = new OrthographicCamera();
		camera.zoom = 0.3f;
		stage = new Stage();
		//stage.setDebugAll(true);
		resize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
		
		WorldConfigurationBuilder builder = new WorldConfigurationBuilder()
			.with(new AssetsSystem(this.game.assetManager))
			.with(new CameraSystem(camera))
			.with(new RenderContextSystem(camera))
			.with(new PlayerDataSystem())
			.with(new CamerafollPlayerPosSystem())
			.with(new PlayerRockerStystem())
			.with(new MovementSystem())
			.with(new AnimationSystem())
			.with(new PlayerTerrainStateSystem())
			.with(new StateSystem())
			.with(new ChunkSystem())
			.with(new ChunkUnloadSystem())
			.with(new NearTagSystem())
			.with(new ChunkGeneraterSystem())
			.with(new SpriteRenderSystem())
			.with(new DebugBoxRenderSystem())
			.with(new NearRenderSystem())
			.with(new DebugUiSystem())
			.with(new StageSystem(stage))
			.with(new PendingDeleteSystem());
			
		world = new World(builder.build());
		inputMultiplexer = new InputMultiplexer();
		inputMultiplexer.addProcessor(stage);
		
	}

	@Override
	public void show()
	{
		super.show();
		Gdx.input.setInputProcessor(inputMultiplexer);
	}
	
	
	@Override
	public void render(float delta)
	{
		ScreenUtils.clear(1,1,1,1);
		world.setDelta(delta);
		world.process();
	}

	@Override
	public void resize(int width, int height) {
		float screenH = GameContext.metersToPixels(GameContext.SCREEN_HEIGHT);
		float screenW = screenH * (width / height);
		camera.setToOrtho(false, screenW, screenH);
		
		stage.getViewport().setScreenSize(width,height);
	}

	@Override
	public void dispose()
	{
		super.dispose();
		world.dispose();
	}
	
	
}
